package com.ktgj.customer.common;

import com.ktgj.common.core.utils.xml.XElement;
import com.ktgj.common.core.utils.xml.XmlUtils;
import com.ktgj.common.core.utils.HttpClientUtil;
import com.ktgj.customer.domain.Insurance.axModel.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class AxInsuranceUtils {

    @Value("${ax-insurance.com-id}")
    private String comId;
    @Value("${ax-insurance.mechanism-vcode}")
    private String mechanismVcode;
    @Value("${ax-insurance.url}")
    private String url;

    /**
     * 安心保险开保
     * @param anXinInsurance
     * @return
     */
    public ApplyResponsePackage openInsurance(AnXinInsurance anXinInsurance) {
        String xmlString = XmlUtils.convertToXml(anXinInsurance).replace("<anXinInsurance>", "").replace("</anXinInsurance>", "").replace("<axApplyInsuranceRequest>", "").replace("</axApplyInsuranceRequest>", "").replace(" standalone=\"yes\"", "").replace("UTF", "utf");
        XElement root = XElement.loadXml(postInsuranceData(xmlString));
        List<proposal> lstProposal = new ArrayList<>();
        ResponseHeader responseHeader = fillResponseHeader(root);
        ApplyResponsePackage applyResponsePackage = new ApplyResponsePackage();
        applyResponsePackage.header = responseHeader;
        if (root.element("packageList").element("ApplyResponsePackage").element("response").elements("proposal") != null) {
            for (XElement elmPro : root.element("packageList").element("ApplyResponsePackage").element("response").elements("proposal")) {
                proposal p = new proposal();
                p.ePolicyUrl = elmPro.element("ePolicyUrl").getValue();
                p.insBeginDate = elmPro.element("insBeginDate").getValue();
                p.insEndDate = elmPro.element("insEndDate").getValue();
                p.mainAmount = elmPro.element("totalAmount").getValue();
                p.orderId = elmPro.element("orderId").getValue();
                p.policyNo = elmPro.element("policyNo").getValue();
                p.prodNo = elmPro.element("policyNo").getValue();
                p.totalPremium = elmPro.element("totalPremium").getValue();
                lstProposal.add(p);
            }
            applyResponsePackage.response = lstProposal;
        }
        return applyResponsePackage;
    }

    /**
     * 安心保险退保
     * @param anXinSurrender
     * @return
     */
    public SurrenderResponsePackage SurrenderInsurance(AnXinSurrender anXinSurrender)
    {
        String xmlString = XmlUtils.convertToXml(anXinSurrender).replace("<anXinSurrender>", "").replace("</anXinSurrender>", "").replace(" standalone=\"yes\"", "").replace("UTF", "utf");
        XElement root = XElement.loadXml(postInsuranceData(xmlString));
        ResponseHeader responseHeader = fillResponseHeader(root);
        SurrenderResponsePackage surrenderResponsePackage = new SurrenderResponsePackage();
        surrenderResponsePackage.header = responseHeader;
        if (root.element("packageList").element("ApplyResponsePackage").element("response").element("orderId") != null) {
            XElement responseElement = root.element("packageList").element("ApplyResponsePackage").element("response");
            ChargeReturn chargeReturn = new ChargeReturn();
            chargeReturn.isSuccess = responseElement.element("isSuccess").getValue();
            chargeReturn.message = responseElement.element("message").getValue();
            chargeReturn.orderId = responseElement.element("orderId").getValue();
            chargeReturn.policyNo = responseElement.element("policyNo").getValue();
            surrenderResponsePackage.response = chargeReturn;
        }
        return surrenderResponsePackage;
    }

    /**
     * 安心自主理赔
     * @param anXinAutoClaim
     * @return
     */
    public AutoClaimResponsePackage AutoClaimInsurance(AXAutoClaimRequest anXinAutoClaim)
    {
        String xmlString = XmlUtils.convertToXml(anXinAutoClaim);
        xmlString = xmlString.replace("<axAutoClaimRequest>", "").replace("</axAutoClaimRequest>", "").replace("<autoClaimCvrgList>", "").replace("</autoClaimCvrgList>", "").replace("AutoClaimRequestPackage", "package").replace("packAgeVo", "package").replace("{无}", "");
        XElement root = XElement.loadXml(postInsuranceData(xmlString));
        AutoClaimResponsePackage autoClaimResponsePackage = new AutoClaimResponsePackage();
        return autoClaimResponsePackage;
    }

//    public AutoClaimResponsePackage AutoClaimInsurance(String xmlString)
//    {
////        xmlString = xmlString.replace("<axAutoClaimRequest>", "").replace("</aXAutoClaimRequest>", "").replace("<autoClaimCvrgList>", "").replace("</autoClaimCvrgList>", "").replace("AutoClaimRequestPackage", "package").replace("{无}", "");
//        XElement root = XElement.loadXml(postInsuranceData(xmlString));
//        AutoClaimResponsePackage autoClaimResponsePackage = new AutoClaimResponsePackage();
//        return autoClaimResponsePackage;
//    }


    /**
     * 填充返回值头信息
     * @param root
     * @return
     */
    private ResponseHeader fillResponseHeader(XElement root) {
        ResponseHeader responseHeader = new ResponseHeader();
        responseHeader.requestType = root.element("packageList").element("ApplyResponsePackage").element("header").element("requestType").getValue();
        responseHeader.responseCode = root.element("packageList").element("ApplyResponsePackage").element("header").element("responseCode").getValue();
        responseHeader.errorMessage = root.element("packageList").element("ApplyResponsePackage").element("header").element("errorMessage").getValue();
        responseHeader.sendTime = root.element("packageList").element("ApplyResponsePackage").element("header").element("sendTime").getValue();
        responseHeader.from = root.element("packageList").element("ApplyResponsePackage").element("header").element("from") == null ? "" : root.element("packageList").element("ApplyResponsePackage").element("header").element("from").getValue();
        responseHeader.orderId = root.element("packageList").element("ApplyResponsePackage").element("header").element("orderId") == null ? "" : root.element("packageList").element("ApplyResponsePackage").element("header").element("orderId").getValue();
        responseHeader.productCode = root.element("packageList").element("ApplyResponsePackage").element("header").element("productCode") == null ? "" : root.element("packageList").element("ApplyResponsePackage").element("header").element("productCode").getValue();
        responseHeader.productName = root.element("packageList").element("ApplyResponsePackage").element("header").element("productName") == null ? "" : root.element("packageList").element("ApplyResponsePackage").element("header").element("productName").getValue();
        return responseHeader;
    }

    private String postInsuranceData(String xmlString)
    {
        //测试
        String comId="EC16070003";
        String urls="https://antx11.95303.com/com.isoftstone.iics.bizsupport.epartner/processOrder";
        String mechanismVcode = "123456";

        //正式
//        String comId = "EC16070003";//EC16070003
//        String mechanismVcode = "jssj_security_answern_16120105";
//        String urls = "http://172.16.2.56:9527/open/processOrder";

//        String md5Request = DigestUtils.md5Hex(this.mechanismVcode + xmlString);
        String md5Request = DigestUtils.md5Hex(mechanismVcode + xmlString);
//        String comId = this.comId;
//        String url = this.url + "?sign=" + md5Request + "&comId=" + comId;
        String url = urls + "?sign=" + md5Request + "&comId=" + comId;
        String resposeString = HttpClientUtil.doPostXmlStream(url, xmlString);
        String responseStr = resposeString.replace("<package>", "<ApplyResponsePackage>").replace("</package>", "</ApplyResponsePackage>").replace("<packageList>", "<AXApplyInsuranceResponse>\r\n<packageList>") + "</AXApplyInsuranceResponse>";
        return responseStr;
    }

}
